class Solution {
public:
    int minCost(string colors, vector<int>& neededTime) {
        int l = 0, r = 1, n = colors.size();
        int sum = neededTime[0], maxV = neededTime[0], ans = 0;
        while (r < n) {
            if (colors[r] == colors[r - 1]) {
                sum += neededTime[r];
                maxV = max(maxV, neededTime[r]);
            }
            else {
                cout << sum << " " << maxV << endl;
                if (r - l > 1)
                    ans += sum - maxV;
                sum = neededTime[r];
                maxV = neededTime[r];
                l = r;
            }
            ++r;
        }
        if (r - l > 1)
            ans += sum - maxV;
        return ans;
    }
};